//
//  TalkRecordingView.m
//  talkDemo
//
//  Created by telen on 15/4/21.
//  Copyright (c) 2015年 刘赞黄Telen. All rights reserved.
//

#import "TalkRecordingView.h"
#import "TalkDefine.h"
#import "Utility.h"

@interface TalkRecordingView ()<TalkVedioPlayDelegate>
{
    AVAudioPlayer* countSound;
    AVAudioPlayer* countSound_end;
}
@end

@implementation TalkRecordingView
{
    NSArray* questionsArr;
    //btnPoint
    CGPoint bpoint_a;
    CGPoint bpoint_b;
    CGPoint bpoint_c;
    //
    UIButton* btnRecord;
    UIButton* btnFinish;
    UIButton* btnStop;
    UIButton* btnNext;
    //
    TalkDoubleLabel* questionLabelView;
    TalkTimeSliderView* timeSlider;
    TalkVideoPlayView* videoView;
    UILabel* coutDownLabel;
    MDRadialProgressView *radialView;
    //
    CGPoint radialShow_pt;
    CGPoint radialUnshow_pt;
    NSTimeInterval radialAnswerTime;
    //
    NSTimer* timer;
    //
    RecordingTimeInfo recordSetInfo;
    //
    BOOL didShow;
}

@synthesize stopRecordBtn = btnStop;
@synthesize finishBtn = btnFinish;
@synthesize reRecordBtn = btnRecord;
@synthesize nextBtn = btnNext;

+ (TalkRecordingView *)TalkRecordingQuestion:(NSArray *)qarr ratio:(CGFloat)ratio_w recordingTime:(RecordingTimeInfo)recordInfo
{
    CGSize ssize = [UIScreen mainScreen].bounds.size;
    CGFloat height = ssize.height > ssize.width ? ssize.height : ssize.width;
    CGRect frame = CGRectMake(0, height, 768/ratio_w, 256/ratio_w);
    return [[TalkRecordingView alloc] initWithFrame:frame questions:(NSArray *)qarr ratio:ratio_w recordingTime:recordInfo];
}

- (instancetype)initWithFrame:(CGRect)frame_self questions:(NSArray *)qarr ratio:(CGFloat)ratio_w recordingTime:(RecordingTimeInfo)recordInfo
{
    if (self = [super initWithFrame:frame_self]) {
        self.ratio = ratio_w;
        questionsArr = qarr;
        recordSetInfo = recordInfo;
        radialShow_pt = CGPointMake(708/ratio_w, 40/ratio_w);
        radialUnshow_pt = CGPointMake(800/ratio_w, 40/ratio_w);
        //
        bpoint_a = CGPointMake(278/ratio_w, 128/ratio_w);
        bpoint_b = CGPointMake(384/ratio_w, 168/ratio_w);
        bpoint_c = CGPointMake(490/ratio_w, 128/ratio_w);
        //
        self.backgroundColor = [UIColor clearColor];
        UIView* blackkuang = [[UIView alloc] init];
        blackkuang.alpha = 0.6f;
        blackkuang.backgroundColor = [UIColor blackColor];
        [self addSubview:blackkuang];
        blackkuang.translatesAutoresizingMaskIntoConstraints = NO;
        [blackkuang lyt_alignToParent];
        
        
        //
        UIImage* image = [UIImage imageNamed:@"talk_btn_record_normal.png"];
        CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
        btnRecord = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnRecord setImage:image forState:UIControlStateNormal];
        [btnRecord setImage:[UIImage imageNamed:@"talk_btn_record_down.png"] forState:UIControlStateHighlighted];
        btnRecord.frame = frame;
        btnRecord.center = bpoint_a;
        btnRecord.hidden = YES;
        [self addSubview:btnRecord];
        [btnRecord addTarget:self action:@selector(reStartRecord) forControlEvents:UIControlEventTouchUpInside];
        //
        btnFinish = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnFinish setImage:[UIImage imageNamed:@"talk_btn_finish_normal.png"] forState:UIControlStateNormal];
        [btnFinish setImage:[UIImage imageNamed:@"talk_btn_finish_down.png"] forState:UIControlStateHighlighted];
        btnFinish.frame = frame;
        btnFinish.center = bpoint_c;
        btnFinish.hidden = YES;
        [self addSubview:btnFinish];
        //
        btnStop = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnStop setImage:[UIImage imageNamed:@"talk_btn_stop_normal.png"] forState:UIControlStateNormal];
        [btnStop setImage:[UIImage imageNamed:@"talk_btn_stop_down.png"] forState:UIControlStateHighlighted];
        btnStop.frame = frame;
        btnStop.center = bpoint_b;
        btnStop.hidden = YES;
        [self addSubview:btnStop];
        [btnStop addTarget:self action:@selector(stopRecord) forControlEvents:UIControlEventTouchUpInside];
        
        //===================
        
        btnNext = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnNext setImage:[UIImage imageNamed:@"talk_btn_next_normal.png"] forState:UIControlStateNormal];
        [btnNext setImage:[UIImage imageNamed:@"talk_btn_next_down.png"] forState:UIControlStateHighlighted];
        btnNext.frame = CGRectMake(0, 0, frame.size.width*0.5, frame.size.height*0.5);
        btnNext.center = CGPointMake(bpoint_b.x+frame.size.width, bpoint_b.y);
        btnNext.hidden = YES;
        btnNext.enabled = NO;
        [self addSubview:btnNext];
        [btnNext addTarget:self action:@selector(nextQuestion) forControlEvents:UIControlEventTouchUpInside];
        
        
        //
        frame = CGRectMake(-frame_self.size.width, 40/ratio_w, frame_self.size.width, 62/ratio_w);
        questionLabelView = [[TalkDoubleLabel alloc] initWithFrame:frame MainText:[qarr objectAtIndex:0] subText:[qarr objectAtIndex:1]  sepRatio:0.56];
        [questionLabelView setTexttextAlignment:NSTextAlignmentCenter];
        [self addSubview:questionLabelView];
        
        //
        frame = CGRectMake(0, 0, frame_self.size.width, 15/ratio_w);
        timeSlider = [[TalkTimeSliderView alloc] initWithFrame:frame color:__TalkBaseBackColor_2 color:__TalkBaseBackColor secondForm:recordInfo.time_from to:recordInfo.time_to point:@[@(recordSetInfo.pTime_1),@(recordSetInfo.pTime_2)]];
        [self addSubview:timeSlider];
        
        //
        frame = CGRectMake(0, 0, 300/ratio_w, 300/ratio_w);
        coutDownLabel = [[UILabel alloc] initWithFrame:frame];
        coutDownLabel.backgroundColor = [UIColor clearColor];
        coutDownLabel.tag = -1;
        coutDownLabel.textAlignment = NSTextAlignmentCenter;
        coutDownLabel.textColor = __TalkBaseBackColor;
        coutDownLabel.font = [UIFont boldSystemFontOfSize:frame.size.width];
        coutDownLabel.center = CGPointMake(frame_self.size.width*0.5, 360/ratio_w);
        coutDownLabel.shadowColor = [UIColor whiteColor];
        coutDownLabel.shadowOffset = CGSizeMake(6/ratio_w, 6/ratio_w);
        
        //
        CGFloat w = 100/ratio_w;
        if ([TalkViewController shareLastInstance].isPhone) {
            w *= 1.2;
        }
        MDRadialProgressTheme *newTheme = [[MDRadialProgressTheme alloc] init];
        newTheme.completedColor = [UIColor colorWithRed:86/255.0 green:103/255.0 blue:198/255.0 alpha:1.0];
        newTheme.incompletedColor = [UIColor colorWithRed:126/255.0 green:143/255.0 blue:218/255.0 alpha:0.2];
        newTheme.centerColor = [UIColor colorWithWhite:1 alpha:0.9];//[UIColor colorWithRed:126/255.0 green:143/255.0 blue:218/255.0 alpha:0.2];
        newTheme.sliceDividerHidden = YES;
        newTheme.labelColor = [UIColor colorWithRed:86/255.0 green:103/255.0 blue:198/255.0 alpha:1.0];
        newTheme.labelShadowColor = [UIColor whiteColor];
        newTheme.font = [UIFont systemFontOfSize:w*0.2];
        newTheme.thickness = 12/ratio_w;
        if ([TalkViewController shareLastInstance].isPhone) {
            newTheme.thickness *= 1.2;
        }
        //
        frame = CGRectMake(0, 0, w*0.6, w*0.6);
        radialView =  [[MDRadialProgressView alloc] initWithFrame:frame andTheme:newTheme];
        radialView.center = radialUnshow_pt;
        //[self addSubview:radialView];
        radialView.progressTotal = 60;
        radialView.isShowCounter = YES;
        radialView.endString = @"'";
        //
        NSString* mp3Path = [[NSBundle mainBundle] pathForResource:@"countdown.mp3" ofType:nil];
        countSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:mp3Path] error:nil];
        mp3Path = [[NSBundle mainBundle] pathForResource:@"countend.mp3" ofType:nil];
        countSound_end = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:mp3Path] error:nil];
    }
    return self;
}

- (void)show:(BOOL)yn countNum:(int)num
{
    if (!coutDownLabel) {
        return;
    }
    if (yn && !coutDownLabel.superview && self.superview) {
        [self.superview addSubview:coutDownLabel];
    }
    if (coutDownLabel.superview) {
        if (yn) {
            coutDownLabel.tag = num;
            coutDownLabel.text = [NSString stringWithFormat:@"%d",num];
            coutDownLabel.transform = CGAffineTransformMakeScale(2, 2);
            [UIView animateWithDuration:0.15 animations:^{
                coutDownLabel.transform = CGAffineTransformMakeScale(1, 1);
            }];
            [countSound stop];
            [countSound play];
        }else{
            [countSound_end stop];
            [countSound_end play];
            [coutDownLabel removeFromSuperview];
        }
    }
}

- (void)show:(BOOL)yn countForAnswer_num:(NSUInteger)numOf100
{
    if (!radialView) {
        return;
    }
    if (yn && !radialView.superview && self.superview) {
        [self.superview addSubview:radialView];
    }
    if (radialView.superview) {
        radialView.progressCounter = numOf100;
        if (yn) {
            if (!CGPointEqualToPoint(radialView.center, radialShow_pt)) {
                [UIView animateWithDuration:0.15 animations:^{
                    radialView.center = radialShow_pt;
                }];
            }
        }else{
            if (CGPointEqualToPoint(radialView.center, radialShow_pt)) {
                [UIView animateWithDuration:0.15 animations:^{
                    radialView.center = radialUnshow_pt;
                }completion:^(BOOL finished) {
                    [radialView removeFromSuperview];
                }];
            }
        }
    }
}

- (void)reStartRecord
{
    [self showBtnForRecordingStatus:YES];
    [self startRecord];
}

- (void)startRecord
{
    timeSlider.countdown_sec = 0;
    [timeSlider show];
    [self performSelector:@selector(startCountDown) withObject:nil afterDelay:0.5];
}

- (void)stopRecord
{
    [self stopCountDown];
    [self show:NO questionIndex:0];
    [self show:NO countForAnswer_num:0];
    [timeSlider unshow];
    [videoView unshow];
    timeSlider.countdown_sec = 0;
}


- (void)nextQuestion
{
    if(_recordingStatus == TalkRecordingStatus_Answer1)
    {
        TLDebugS(@"Next!");
        timeSlider.countdown_sec = recordSetInfo.pTime_1;
    }
    
    else if(_recordingStatus == TalkRecordingStatus_Answer2)
    {
        TLDebugS(@"Next!");
        timeSlider.countdown_sec = recordSetInfo.pTime_2;
    }
    
    else if(_recordingStatus == TalkRecordingStatus_Answer3)
    {
        TLDebugS(@"Next!");
        timeSlider.countdown_sec = recordSetInfo.time_to;
    }
}

- (void)startCountDown
{
    [timer invalidate];
    timer = [NSTimer timerWithTimeInterval:1.f/60 target:self selector:@selector(timeTick:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    _recordingStatus = TalkRecordingStatus_begin;
    if (_delegate && [_delegate respondsToSelector:@selector(talkRecordingView_Countdown_Start:)]) {
        [_delegate talkRecordingView_Countdown_Start:self];
    }
}

- (void)stopCountDown
{
    _recordingStatus = TalkRecordingStatus_end;
    [timer invalidate],timer = nil;
    [self show:NO countNum:-1];
    if (_delegate && [_delegate respondsToSelector:@selector(talkRecordingView_Countdown_Stop:)]) {
        [_delegate talkRecordingView_Countdown_Stop:self];
    }
}

- (void)timeTick:(NSTimer*)tmr
{
    if (!didShow) {
        [self stopCountDown];
        timeSlider.countdown_sec = 0;
        return;
    }
    if (timeSlider && _recordingStatus < TalkRecordingStatus_end) {
        timeSlider.countdown_sec += tmr.timeInterval;
        
        //========================================
        //Blink red if time remaining in each segment <= 20%
        //========================================
        float alpha = 0;
        CGFloat maxGap = 1; CGFloat gap = 0;
        if (_recordingStatus == TalkRecordingStatus_Question1
            ||_recordingStatus == TalkRecordingStatus_Answer1)
        {
            maxGap = recordSetInfo.pTime_1 - recordSetInfo.time_from;
            gap = timeSlider.countdown_sec;
        }
        
        else if (_recordingStatus == TalkRecordingStatus_Question2
                 ||_recordingStatus == TalkRecordingStatus_Answer2)
        {
            maxGap = recordSetInfo.pTime_2 - recordSetInfo.pTime_1;
            gap = timeSlider.countdown_sec - (recordSetInfo.pTime_1 - recordSetInfo.time_from);
        }
        else if (_recordingStatus == TalkRecordingStatus_Question3
                 ||_recordingStatus == TalkRecordingStatus_Answer3)
        {
            maxGap = recordSetInfo.time_to - recordSetInfo.pTime_2;
            gap = timeSlider.countdown_sec - (recordSetInfo.pTime_2 - recordSetInfo.time_from);
        }
        if(gap / maxGap >= 0.8f)
        {
            CGFloat base = 0.8f * maxGap;
            gap = gap - base;
            alpha = (sinf(gap * M_PI_2) + 1) / 2;
        }
        
        int r = 124 + (255 - 124) * alpha;
        int g = 132 + (0 - 132) * alpha;
        int b = 197 + (0 - 197) * alpha;
        [timeSlider setForeColor:[UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]];
        
        //========================================
        
        
        //---
        if (_recordingStatus == TalkRecordingStatus_Answer1
            || _recordingStatus == TalkRecordingStatus_Answer2
            || _recordingStatus == TalkRecordingStatus_Answer3){
            radialView.theme.completedColor = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1];
            [self show:YES countForAnswer_num:radialView.progressTotal-(timeSlider.countdown_sec - radialAnswerTime)];
            //btnNext.enabled = YES;
        }
        //---
        
        if (_recordingStatus < TalkRecordingStatus_Question1 && timeSlider.countdown_sec < recordSetInfo.time_from) {
            int cut = recordSetInfo.time_from - timeSlider.countdown_sec;
            cut++;
            if (coutDownLabel.tag != cut) {
                [self show:YES countNum:cut];
                btnNext.enabled = NO;
            }
        }
        else if (_recordingStatus < TalkRecordingStatus_Question1 && timeSlider.countdown_sec > recordSetInfo.time_from) {
            [self show:NO countNum:-1];
            _recordingStatus = TalkRecordingStatus_Question1;
            [self playVedioAndSoundForQuestionIndex:0];
            [self show:YES questionIndex:0];
            [self show:NO countForAnswer_num:0];
            btnNext.enabled = NO;
        }
        
        else if (_recordingStatus < TalkRecordingStatus_Question2 && timeSlider.countdown_sec > recordSetInfo.pTime_1) {
            if (_delegate && [_delegate respondsToSelector:@selector(talkRecordingView:answerEnd_Index:)]) {
                [_delegate talkRecordingView:self answerEnd_Index:0];
            }
            _recordingStatus = TalkRecordingStatus_Question2;
            [self playVedioAndSoundForQuestionIndex:1];
            [self show:YES questionIndex:1];
            [self show:NO countForAnswer_num:0];
            btnNext.enabled = NO;
        }
        else if (_recordingStatus < TalkRecordingStatus_Question3 && timeSlider.countdown_sec > recordSetInfo.pTime_2) {
            if (_delegate && [_delegate respondsToSelector:@selector(talkRecordingView:answerEnd_Index:)]) {
                [_delegate talkRecordingView:self answerEnd_Index:1];
            }
            _recordingStatus = TalkRecordingStatus_Question3;
            [self playVedioAndSoundForQuestionIndex:2];
            [self show:YES questionIndex:2];
            [self show:NO countForAnswer_num:0];
            btnNext.enabled = NO;
        }
        else if (timeSlider.progress >= 1 || timeSlider.countdown_sec >= recordSetInfo.time_to) {
            if (_delegate && [_delegate respondsToSelector:@selector(talkRecordingView:answerEnd_Index:)]) {
                [_delegate talkRecordingView:self answerEnd_Index:2];
            }
            [self showBtnForRecordingStatus:NO];
            [self show:NO questionIndex:0];
            [self stopCountDown];
            _recordingStatus = TalkRecordingStatus_end;
            [self show:NO countForAnswer_num:0];
            btnNext.enabled = NO;
        }
    }
}

- (void)playVedioAndSoundForQuestionIndex:(int)index
{
//    return;
    if (_delegate) {
        [Utility setAudioCategory:AVAudioSessionCategoryPlayAndRecord];
        //[Utility setAudioCategory:AVAudioSessionCategoryPlayback];
        
        NSString* mp4Path = [_delegate talkRecordingView:self mp4ForQuestionIndex:index];
        NSString* mp3Path = [_delegate talkRecordingView:self mp3ForQuestionIndex:index];
        if (!videoView) {
            CGSize ssize = [UIScreen mainScreen].bounds.size;
            CGFloat height = ssize.height > ssize.width ? ssize.height : ssize.width;
            //
            CGSize size = CGSizeMake(180/self.ratio, 240/self.ratio);
            if ([TalkViewController shareLastInstance].isPhone) {
                size.height *= 1.2;
                size.width *= 1.2;
            }
            videoView = [TalkVideoPlayView talkVedioPlayView_withSize:size];
            videoView.center = CGPointMake(64/self.ratio, height-268/self.ratio);
            videoView.hidden = YES;
            videoView.delegate = self;
            [self.superview addSubview:videoView];
        }
        [videoView playMp4:mp4Path andMp3:mp3Path sign:[NSString stringWithFormat:@"%d",index]];
        if (_delegate && [_delegate respondsToSelector:@selector(talkRecordingView:questionBegin_Index:)]) {
            [_delegate talkRecordingView:self questionBegin_Index:index];
        }
    }
}

- (void)show:(BOOL)yn questionIndex:(int)index
{
    CGRect frame1 = questionLabelView.frame;
    CGRect frame2 = frame1;
    CGRect frame3 = frame1;
    frame1.origin.x = -frame1.size.width;
    frame2.origin.x =  0;
    frame3.origin.x = frame1.size.width;
    [UIView animateKeyframesWithDuration:0.6f delay:0.0f options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.3 animations:^{
            questionLabelView.frame = frame1;
        }];
        if (yn) {
            [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.0 animations:^{
                questionLabelView.frame = frame3;
                questionLabelView.label_main.text = [questionsArr objectAtIndex:index*2];
                questionLabelView.label_sub.text = [questionsArr objectAtIndex:index*2+1];
            }];
            [UIView addKeyframeWithRelativeStartTime:0.31 relativeDuration:0.6 animations:^{
                questionLabelView.frame = frame2;
            }];
        }
    } completion:^(BOOL finished) {
        
    }];
}

- (void)showBtnForRecordingStatus:(BOOL)yn
{
    if (yn) {
        [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
            [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.3 animations:^{
                btnStop.hidden = NO;
                btnStop.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
                
                btnNext.hidden = NO;
                btnNext.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
                
            }];
            [UIView addKeyframeWithRelativeStartTime:0.7 relativeDuration:0.3 animations:^{
                btnStop.transform = CGAffineTransformMakeScale(1, 1);
                btnNext.transform = CGAffineTransformMakeScale(1, 1);
            }];
            //
            [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.3 animations:^{
                btnFinish.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
                btnRecord.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
            }];
            [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.2 animations:^{
                btnFinish.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
                btnRecord.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
            }];
        } completion:^(BOOL finished) {
            btnStop.hidden = NO;
            btnNext.hidden = NO;
            btnFinish.hidden = YES;
            btnRecord.hidden = YES;
        }];
    }else{
        [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
            [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.3 animations:^{
                btnFinish.hidden = NO;
                btnFinish.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
                btnRecord.hidden = NO;
                btnRecord.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
            }];
            [UIView addKeyframeWithRelativeStartTime:0.7 relativeDuration:0.3 animations:^{
                btnFinish.transform = CGAffineTransformMakeScale(1, 1);
                btnRecord.transform = CGAffineTransformMakeScale(1, 1);
            }];
            //
            [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.3 animations:^{
                btnStop.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
                btnNext.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
            }];
            [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.2 animations:^{
                btnStop.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
                btnNext.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
            }];
        } completion:^(BOOL finished) {
            btnStop.hidden = YES;
            btnNext.hidden = YES;
            btnFinish.hidden = NO;
            btnRecord.hidden = NO;
        }];
    }
}



- (void)show
{
    CGRect frame = self.frame;
    CGSize ssize = [UIScreen mainScreen].bounds.size;
    CGFloat height = ssize.height > ssize.width ? ssize.height : ssize.width;
    frame.origin = CGPointMake(frame.origin.x, height-256/self.ratio);
    btnStop.hidden = YES;
    btnNext.hidden = YES;
    btnRecord.hidden = YES;
    btnFinish.hidden = YES;
    btnStop.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
    btnNext.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
    btnRecord.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
    btnFinish.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
    [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.3 animations:^{
            self.frame = frame;
        }];
        [UIView addKeyframeWithRelativeStartTime:0.2 relativeDuration:0.3 animations:^{
            [self showBtnForRecordingStatus:YES];
        }];
    } completion:^(BOOL finished) {
        didShow = YES;
        [self startRecord];
    }];
    
    self.isShown = YES;
}

- (void)unshow
{
    didShow = NO;
    CGRect frame = self.frame;
    CGSize ssize = [UIScreen mainScreen].bounds.size;
    CGFloat height = ssize.height > ssize.width ? ssize.height : ssize.width;
    frame.origin = CGPointMake(frame.origin.x, height);
    [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.2 animations:^{
            btnStop.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
            btnNext.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
            btnRecord.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
            btnFinish.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
        }];
        [UIView addKeyframeWithRelativeStartTime:0.2 relativeDuration:0.3 animations:^{
            btnStop.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
            btnNext.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
            btnRecord.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
            btnFinish.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
        }];
        [UIView addKeyframeWithRelativeStartTime:0.7 relativeDuration:0.3 animations:^{
            self.frame = frame;
        }];
    } completion:^(BOOL finished) {

    }];
    
    self.isShown = NO;
}

- (void)TalkVedioPlay_DidPlayEnd_sign:(NSString *)sign
{
    int index = [sign intValue];
    NSUInteger leaveSec = 0;
    //
    switch (index) {
        case 0:
            leaveSec = recordSetInfo.pTime_1 -timeSlider.countdown_sec;
            break;
        case 1:
            leaveSec = recordSetInfo.pTime_2 -timeSlider.countdown_sec;
            break;
        case 2:
            leaveSec = recordSetInfo.time_to -timeSlider.countdown_sec;
            break;
        default:
            leaveSec = 0;
            break;
    }
    radialAnswerTime = timeSlider.countdown_sec;
    radialView.progressTotal = leaveSec+1;
    [self show:YES countForAnswer_num:leaveSec];
    
    if (_recordingStatus == TalkRecordingStatus_Question1+index*2) {
        [Utility setAudioCategory:AVAudioSessionCategoryRecord];
        [self performSelector:@selector(btnNext_Yes) withObject:nil afterDelay:0.33f];
        if (_delegate && [_delegate respondsToSelector:@selector(talkRecordingView:questionEnd_Index:)]) {
            [_delegate talkRecordingView:self questionEnd_Index:index];
        }
        if (_delegate && [_delegate respondsToSelector:@selector(talkRecordingView:answerBegin_Index:)]) {
            [_delegate talkRecordingView:self answerBegin_Index:index];
        }
    }
    _recordingStatus++;
}

- (void)btnNext_Yes
{
    btnNext.enabled= YES;
}

@end
